package com.he.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.pojo.LogComment;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.pojo.UserLog;
import com.he.pojo.UserLogExample;
import com.he.service.department.DepartmentServiceI;
import com.he.service.logComment.LogCommentServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.userLog.UserLogServiceI;

@Controller
@RequestMapping("log")
public class LogController {

	@Autowired
	UserLogServiceI logService;
	@Autowired
	UserServiceI userService;
	@Autowired
	DepartmentServiceI deptservice;
	@Autowired
	LogCommentServiceI comService;
	@RequestMapping("show")
	public String logShow(HttpServletRequest request,HttpSession session,UserLog log) {
		User user=(User) session.getAttribute("user");
		System.out.println(user.getUserName()+"----------------------");
		int departId=user.getDepartmentId();
		User user1=new User();
		user1.setWithPage(0);
		UserExample userExample=new UserExample();
		userExample.createCriteria().andDepartmentIdEqualTo(departId);
		List<User> users = userService.getUsers(userExample, user1);
		
		UserLog userLog=new UserLog();
		user1.setWithPage(0);
		List<UserLog> logs=new ArrayList<>();
		for (User use : users) {
			UserLogExample userLogExample=new UserLogExample();
			userLogExample.createCriteria().andUserIdEqualTo(use.getId());
			List<UserLog> list=logService.getUserLogs(userLogExample, userLog);
			for (UserLog userLog2 : list) {
				userLog2.setUserName(use.getUserName());
				logs.add(userLog2);
			}
		}
		request.setAttribute("list", logs);
		/*为log总页数赋值,向前端传总页数*/
		PageInfo<UserLog> info=new PageInfo<>(logs);
		//为log总页数赋值
		log.setPages(info.getPages());
		//为log总记录数赋值
		log.setTotal(info.getTotal());
		/*为log返回的controller链接赋值*/
		log.setUrl("show.do");
		/*向前端传参数保存在log中*/
		request.setAttribute("log", log);
		return "logManage";
	}
	@RequestMapping("showMe")
	public String logShowMe(HttpServletRequest request, HttpSession session,UserLog log) {
		User user=(User) session.getAttribute("user");
		System.out.println(user.getUserName()+"----------------------");
		int userId=user.getId();
		UserLog userLog=new UserLog();
		userLog.setWithPage(0);
		List<UserLog> logs=new ArrayList<>();
		UserLogExample userLogExample=new UserLogExample();
		userLogExample.createCriteria().andUserIdEqualTo(userId);
		List<UserLog> list=logService.getUserLogs(userLogExample, userLog);
		User use=userService.getUserById(userId);
		for (UserLog userLog2 : list) {
				userLog2.setUserName(use.getUserName());
				logs.add(userLog2);
			
		}
		request.setAttribute("list", logs);
		/*为log总页数赋值,向前端传总页数*/
		PageInfo<UserLog> info=new PageInfo<>(logs);
		//为log总页数赋值
		log.setPages(info.getPages());
		//为log总记录数赋值
		log.setTotal(info.getTotal());
		/*为log返回的controller链接赋值*/
		log.setUrl("show.do");
		/*向前端传参数保存在log中*/
		request.setAttribute("log", log);
		return "myLog";
	}
	
	@RequestMapping("detail")
	public String detail(int id,HttpServletRequest request) {
		UserLog log=logService.getUserLogById(id);
		User user=userService.getUserById(log.getUserId());
		Department dept=deptservice.getDepartmentById(user.getDepartmentId());
		request.setAttribute("log", log);
		request.setAttribute("user", user);
		request.setAttribute("dept", dept);
		return "logShow";
	}
	@RequestMapping("del")
	public String delete(int id) {
		logService.delete(id);
		return "redirect:showMe.do";
	}
	
	@RequestMapping("add")
	public String addLink() {
		
		return "addLog";
	}
	@RequestMapping("addDo")
	@ResponseBody
	public String addLog(UserLog userLog,HttpSession session) {
		User user=(User) session.getAttribute("user");
		System.out.println(user.getUserName()+"----------------------");
		Date date=new Date();
		userLog.setUserId(user.getId());
		userLog.setLogDate(date);
		userLog.setCommentNum(0);
		logService.insert(userLog);
		System.out.println(userLog.getLogTitle()+"--------------");
		return "success";
	}
	@RequestMapping("commentDo")
	@ResponseBody
	public String addComment(LogComment logComment) {
		Date date=new Date();
		logComment.setCommentDate(date);
		comService.insert(logComment);
		UserLog userLog=logService.getUserLogById(logComment.getLogId());
		int num=userLog.getCommentNum()+1;
		userLog.setCommentNum(num);
		logService.update(userLog);
		return "success";
	}
	
}
